<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author PR
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button
          type="default"
          @click="handleExport()"
          v-if="props.status == '4'"
          v-auth="'asd:asdTm:dc'"
        >
          <Icon icon="ant-design:download-outlined" /> {{ t('导出') }}
        </a-button>
      </template>
    </BasicTable>
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsAsdTlAsdTmTlList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useGlobSetting } from '/@/hooks/setting';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { downloadByUrl } from '/@/utils/file/download';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { asdTmTlListData, fileDataUpload } from '/@/api/asd/tl/asdTmTl';
  import { FormProps } from '/@/components/Form';

  const { ctxAdminPath } = useGlobSetting();
  const { t } = useI18n('asd.tl.asdTmTl');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('退料申请管理'),
  };

  const props = defineProps({
    status: { type: String, default: '0' },
  });

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('物料编码'),
        field: 'basInv.cinvcode',
        component: 'Input',
      },
      {
        label: t('物料代码'),
        field: 'basInv.cinvaddcode',
        component: 'Input',
      },
      {
        label: t('物料名称'),
        field: 'basInv.cinvname',
        component: 'Input',
      },
      {
        label: t('仓库'),
        field: 'cwhcode',
        component: 'Input',
      },
      {
        label: t('批次'),
        field: 'cbatch',
        component: 'Input',
      },
      {
        label: t('状态'),
        field: 'status',
        component: 'Select',
        componentProps: {
          dictType: 'ASD_ROW_STATUS ',
          allowClear: true,
          onChange: handleSuccess,
        },
        ifShow: false,
      },
      {
        label: t('备注'),
        field: 'remarks',
        component: 'Input',
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('单据号'),
      dataIndex: 'djno',
      key: 'a.djno',
      sorter: true,
      width: 100,
      align: 'left',
    },
    {
      title: t('状态'),
      dataIndex: 'status',
      key: 'a.status',
      sorter: true,
      width: 80,
      align: 'center',
      dictType: 'ASD_ROW_STATUS ',
    },
    {
      title: t('导出次数'),
      dataIndex: 'printQty',
      key: 'a.printQty',
      sorter: true,
      width: 90,
      align: 'right',
    },
    {
      title: t('物料编码'),
      dataIndex: 'cinvcode',
      key: 'a.cinvcode',
      sorter: true,
      width: 140,
      align: 'left',
    },
    {
      title: t('物料代码'),
      dataIndex: 'basInv.cinvaddcode',
      key: 'inv.cinvaddcode',
      sorter: true,
      width: 130,
      align: 'left',
      defaultHidden: true,
    },
    {
      title: t('物料名称'),
      dataIndex: 'basInv.cinvname',
      key: 'inv.cinvname',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('批次'),
      dataIndex: 'cbatch',
      key: 'a.cbatch',
      sorter: true,
      width: 80,
      align: 'left',
    },
    {
      title: t('退料数'),
      dataIndex: 'iqty',
      key: 'a.iqty',
      sorter: true,
      width: 80,
      align: 'right',
    },
    {
      title: t('仓库'),
      dataIndex: 'cwhcode',
      key: 'a.cwhcode',
      sorter: true,
      width: 90,
      align: 'left',
    },
    {
      title: t('实集号'),
      dataIndex: 'retrace',
      key: 'a.retrace',
      sorter: true,
      width: 140,
      align: 'left',
    },
  ];

  const [registerTable, { getSelectRows }] = useTable({
    api: asdTmTlListData,
    beforeFetch: (params) => {
      params['status'] = props.status;
      return params;
    },
    columns: tableColumns,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    rowSelection: {
      type: 'checkbox',
    },
  });

  async function handleExport() {
    if (getSelectRows().length) {
      var selIds = ref('');
      getSelectRows().forEach((item) => {
        selIds.value += item.id + ',';
      });

      const res = await fileDataUpload({ selIds: selIds.value });
      if (res.result == 'true') {
        downloadByUrl({ url: ctxAdminPath + '/file/download/' + res.data });
      } else {
        showMessage(res.message);
      }
    } else {
      showMessage('请选择一行数据！！');
    }
  }

  function handleSuccess() {
    reload();
  }
</script>
